﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Collections.Specialized;
using System.IO;
using System.Text.RegularExpressions;

namespace bezlimitu.App_Code
{

    public class NetloadAgent : Agent
    {
        private static string LoginUrl = "http://netload.in/index.php";
        private static string LogoutUrl = "http://netload.in/index.php";
        protected String Url { get; set; }
        protected String Username { get; set; }
        protected String Password { get; set; }
        protected String DirectUrl { get; set; }
        protected CookieCollection cookie;
        protected long FileSize { get; set; }
        protected String FileName { get; set; }
        
        public NetloadAgent(String Url, String Username, String Password)
        {
            this.Url = Url;
            this.Username = Username;
            this.Password = Password;
            this.DirectUrl = String.Empty;
            this.FileSize = 0;
        }

        public NetloadAgent(String Url)
        {
            this.Url = Url;
            this.DirectUrl = String.Empty;
            this.FileSize = 0;
        }

        private String getID(String link)
        {
            Match match = Regex.Match(link, @"/datei([a-zA-Z0-9]+)", RegexOptions.IgnoreCase);
           	if (match.Success)
	            return match.Groups[1].Value;
            match = Regex.Match(link, @"file_id=([a-zA-Z0-9]+)", RegexOptions.IgnoreCase);
            if (match.Success)
                return match.Groups[1].Value;
            match = Regex.Match(link, @"netload\\.in/([a-zA-Z0-9]+)/.?+", RegexOptions.IgnoreCase);
            if (match.Success)
                return match.Groups[1].Value;
            return String.Empty;
        }

        public override bool LogIn()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://api.netload.in/user_info.php?auth=ktPvHFc3ile6qU9TYNKN28geKSfZ0h3e&user_id=" + System.Web.HttpUtility.HtmlEncode(Username) + "&user_password=" + System.Web.HttpUtility.HtmlEncode(Password));
            webRequest.Method = "GET";
            webRequest.CookieContainer = new CookieContainer();
            HttpWebResponse Response = (HttpWebResponse)webRequest.GetResponse();
            

            Response = (HttpWebResponse)webRequest.GetResponse();
            string responseData = string.Empty;

            using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
            {
                responseData = responseReader.ReadToEnd();
            }
            cookie = Response.Cookies;
            if (responseData.Contains("disallowed_agent") || responseData.Contains("unknown_auth"))
            {
                //TODO:
                //PROBLEM API , SERWIS NIE DZIAŁA
                return false;
            }
            else if (responseData.Contains("No premium user"))
            {
                //TODO:
                //KONTO NIE AKTYWNE
                return false;
            }
            else if (responseData.Contains("login_failed"))
            {
                //TODO:
                //BLEDNY LOGIN LUB HASLO
                return false;
            }
            foreach (Cookie c in cookie)
                System.Diagnostics.Debug.WriteLine(c.Name + ":" + c.Value);
            return true;
        }

        public override bool LogOut()
        {
            return true;
        }

        public override DownloadLink GetFileInfo()
        {
        //    return getID(Url);
            return null;
        }

    }
}